package wheelview.DataChoose;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.concerneyes.R;

import java.util.ArrayList;
import java.util.List;

import util.ActivityUtil;
import view.MyUniversalDialog;
import wheelview.widget.OnWheelChangedListener;
import wheelview.widget.WheelView;


public class DialogData implements View.OnClickListener, OnWheelChangedListener {
    private WheelView dataView;
    private List<String> datas = new ArrayList<>();
    private String data;
    private TextView btnSave;
    private TextView title;
    private ImageView pay_cancel;

    private DataAdapter adapter;
    private final int TEXTSIZE = ActivityUtil.sp2px(6);//选择器的字体大小
    private Context context;
    private OnChooseDataSuccessListener listener;
    private MyUniversalDialog dialog;
    private int width;
    private int currentData = 0;
    private String titleStr = "";

    public DialogData(Context context, OnChooseDataSuccessListener listener,
                      int width, List<String> datas,String title) {
        this.context = context;
        this.listener = listener;
        this.width = width;
        this.datas = datas;
        this.titleStr = title;
        showChooseDataDialog();
    }

    public String getTitleStr() {
        return titleStr;
    }
    public void dialogShow() {
        dialog.show();
    }

    private void showChooseDataDialog() {
        dialog = new MyUniversalDialog(context);
        dialog.setLayout(LayoutInflater.from(context).inflate(R.layout.dialog_data, null),
                MyUniversalDialog.DialogGravity.CENTER, -2, width);
        dialog.show();
        dataView = (WheelView) dialog.findViewById(R.id.dataView);
        btnSave = (TextView) dialog.findViewById(R.id.btn_save);
        title = (TextView) dialog.findViewById(R.id.title);
        pay_cancel = (ImageView) dialog.findViewById(R.id.pay_cancel);
        btnSave.setOnClickListener(this);
        pay_cancel.setOnClickListener(this);
        // 设置可见条目数量
        dataView.setVisibleItems(3);
        // 添加change事件
        dataView.addChangingListener(this);
        title.setText(titleStr);
        initData();

    }

    private void initData() {
        //初始化数据
        adapter = new DataAdapter(context, datas, currentData);
        adapter.setTextSize(TEXTSIZE);//设置字体大小
        adapter.setCurrentIndex(0);
        dataView.setViewAdapter(adapter);
        data = datas.get(dataView.getCurrentItem());
    }
    public void setDate(String data) {
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_save:
                listener.chooseDataSuccess(data);
                dialog.dismiss();
            case R.id.pay_cancel:
                dialog.dismiss();
        }
    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if (wheel == dataView) {
            data = datas.get(newValue);
            adapter.setCurrentIndex(newValue);
        }
    }


    public interface OnChooseDataSuccessListener {
        void chooseDataSuccess(String data);

        void onChooseDataFail();
    }
}
